﻿using CarPCBATest.Procedure;
using Common;
using System;

namespace CarPCBATest.ViewModel
{
    internal class LampTestViewModel : NotifyBase
    {
        #region Dx11

        private bool _dx11PosLed;

        public bool Dx11PosLed
        {
            get { return _dx11PosLed; }
            set
            {
                _dx11PosLed = value;
                Mylog.Info($"位置灯[{value}]");
                PcbCANControl.PosLampStatus = value;
                this.DoNotify();
            }
        }

        private bool _dxllBreakLed;

        public bool Dx11BreakLed
        {
            get { return _dxllBreakLed; }
            set
            {
                Mylog.Info($"刹车灯[{value}]");
                PcbCANControl.BreakLampStatus = value;
                _dxllBreakLed = value;
                this.DoNotify();
            }
        }

        private bool _dx11TurnLeftLed;

        public bool Dx11TurnLeftLed
        {
            get { return _dx11TurnLeftLed; }
            set
            {
                Mylog.Info($"左转向灯[{value}]");
                if (value)
                {
                    if (_dx11TurnLeftLed)
                    {
                        PcbCANControl.TurnLampStatus = TurnLampStatus.AllOn;
                    }
                    else
                    {
                        PcbCANControl.TurnLampStatus = TurnLampStatus.OnlyLeftOn;
                    }
                }
                else
                {
                    if (_dx11TurnLeftLed)
                    {
                        PcbCANControl.TurnLampStatus = TurnLampStatus.OnlyRightOn;
                    }
                    else
                    {
                        PcbCANControl.TurnLampStatus = TurnLampStatus.AllOff;
                    }
                }
                _dx11TurnLeftLed = value;
                this.DoNotify();
            }
        }

        private bool _dx11TurnRightLed;
        //private bool loaded;

        public bool Dx11TurnRightLed
        {
            get { return _dx11TurnRightLed; }
            set
            {
                Mylog.Info($"左转向灯[{value}]");
                if (value)
                {
                    if (_dx11TurnLeftLed)
                    {
                        PcbCANControl.TurnLampStatus = TurnLampStatus.AllOn;
                    }
                    else
                    {
                        PcbCANControl.TurnLampStatus = TurnLampStatus.OnlyRightOn;
                    }
                }
                else
                {
                    if (_dx11TurnLeftLed)
                    {
                        PcbCANControl.TurnLampStatus = TurnLampStatus.OnlyLeftOn;
                    }
                    else
                    {
                        PcbCANControl.TurnLampStatus = TurnLampStatus.AllOff;
                    }
                }
                _dx11TurnRightLed = value;
                this.DoNotify();
            }
        }

        #endregion Dx11

        public CommandBase DxllControlCommand { get; set; }

        public CommandBase RcmControlCommand { get; set; }
        public CommandBase UnloadedCommand { get; set; }
        public CommandBase LoadedCommand { get; set; }

        public LampTestViewModel()
        {
            DxllControlCommand = new CommandBase(DxllControl);
            RcmControlCommand = new CommandBase(RcmControl);
            LoadedCommand = new CommandBase(Loaded);
            UnloadedCommand = new CommandBase(Unloaded);
        }

        private void Loaded(object obj)
        {
            //loaded = true;
        }

        private void Unloaded(object obj)
        {
            //PcbCANControl.DisConnect();
        }

        private void RcmControl(object obj)
        {
            try
            {
                var index = (LampTestControl)obj;
                switch (index)
                {
                    case LampTestControl.AirCylinderWork:
                        break;

                    case LampTestControl.AirCylinderFree:
                        break;

                    case LampTestControl.RcmConnect:

                        PcbCANControl.Connect();
                        break;

                    case LampTestControl.RcmClose:
                        PcbCANControl.DisConnect();
                        break;

                    default:
                        break;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void DxllControl(object obj)
        {
            var index = (Dx11Led)obj;
            switch (index)
            {
                case Dx11Led.PosLedOn:
                    Mylog.Info("打开位置灯");
                    PcbCANControl.PosLampStatus = true;
                    break;

                case Dx11Led.PosLedOff:
                    Mylog.Info("关闭位置灯");
                    PcbCANControl.PosLampStatus = false;
                    break;

                case Dx11Led.TurnLeftLedOn:
                    Mylog.Info("打开左转向");
                    PcbCANControl.TurnLampStatus = TurnLampStatus.OnlyLeftOn;
                    break;

                case Dx11Led.TurnRightLedOn:
                    Mylog.Info("打开右边转向");
                    PcbCANControl.TurnLampStatus = TurnLampStatus.OnlyRightOn;
                    break;

                case Dx11Led.TurnLedAllOn:
                    Mylog.Info("打开左右转向");
                    PcbCANControl.TurnLampStatus = TurnLampStatus.AllOn;
                    break;

                case Dx11Led.TurnLedAllOff:
                    Mylog.Info("关闭左右转向");
                    PcbCANControl.TurnLampStatus = TurnLampStatus.AllOff;
                    break;

                case Dx11Led.BreakLedOn:
                    Mylog.Info("打开刹车灯");
                    PcbCANControl.BreakLampStatus = true;
                    break;

                case Dx11Led.BreakLedOff:
                    Mylog.Info("关闭刹车灯");
                    PcbCANControl.BreakLampStatus = false;
                    break;

                default:
                    break;
            }
        }
    }
}